package com.css.dzj.dzjap.modules.lawenforcesuper.controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.info.entity.ApExpertEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.entity.ApSupersiveExpertInfoEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.service.SupersiveExpertService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 专家抽取抽取controller
 * @author limanman
 */
@RequestMapping("/supersiveExpert")
@RestController
public class SupersiveExpertController {
    @Autowired
    private SupersiveExpertService supersiveExpertService;
    @Autowired
    BaseService baseService;
    /**
     * 保存抽取专家的两张表
     * @param list
     * @return
     */
    @PostMapping("/saveSupersiveExpert")
    public R saveSupersiveExpert(@RequestBody List<ApSupersiveExpertInfoEntity> list, HttpServletRequest request){
        try {
            R r = supersiveExpertService.saveSupersiveExpert(list,request);
            if (r.getCode()=="500"){
                baseService.logFailed(3,"查询失败","2");
                return R.error(r.getMsg());
            }else {
                baseService.logSuccess(3,"查询成功","2");
                return R.ok();
            }
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3,"查询失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 查询抽取的专家
     * @param taskId
     * @param recordId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("/querySupersiveExpertOnly")
    public R querySupersiveExpertOnly(@RequestParam( name = "taskId",required = false) String taskId,
                                   @RequestParam(name = "recordId",required = false) String recordId,
                                   @RequestParam(name = "pageNo",required = false) Integer pageNo,
                                   @RequestParam(name = "pageSize",required = false) Integer pageSize){
        try {
            List<ApSupersiveExpertInfoEntity> apSupersiveExpertInfoEntities = supersiveExpertService.querySupersiveExpertOnly(taskId,recordId);

            PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
            if (!StringUtils.isEmpty(apSupersiveExpertInfoEntities)&&apSupersiveExpertInfoEntities.size()>0){
                QueryResults<ApExpertEntity> apExpertEntities = supersiveExpertService.queryExpertById(pageRequest,recordId,taskId);
                baseService.logSuccess(3,"查询成功","2");
                return R.ok().put("data",apExpertEntities).put("apSupersiveExpertInfoEntities",apSupersiveExpertInfoEntities);
            }
            baseService.logFailed(3,"查询失败","2");
            return R.error();
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3,"查询失败","2");
            return R.error();
        }
    }


    @PostMapping("/list")
    public R list(@RequestBody List<ApSupersiveExpertInfoEntity> list) {
        System.out.println(list);
        return R.ok();
    }
}
